﻿namespace PowerSong.SongDatabase.ObjectModel
    {
    using System;
    using System.Collections.Generic;

    public class Styles : ObjectModelBase
        {

        internal Styles(Database database) : base(database) { }

        public Style[] GetAllStyles()
            {

            var result = new List<Style>();
            foreach (var nextStyle in Database.Filter<Style>())
                {
                result.Add(nextStyle);
                }
            return result.ToArray();

            }

        public int Count()
            {
            return Database.Filter<Style>().Count;
            }

        public Style Get(Guid identifier)
            {
            return Database.Get<Style>(identifier);
            }

        public Style Get(string name, bool caseSensitive = false)
            {
            foreach (var style in Database.Filter<Style>())
                {
                if (caseSensitive)
                    {
                    if (style.Name == name) return style;
                    }
                else
                    {
                    if (style.Name.ToUpper() == name.ToUpper()) return style;
                    }
                }
            return null;
            }

        public bool Contains(string name, bool caseSensitive = false)
            {
            foreach (var style in Database.Filter<Style>())
                {
                if (caseSensitive)
                    {
                    if (style.Name == name) return true;
                    }
                else
                    {
                    if (style.Name.ToUpper() == name.ToUpper()) return true;
                    }
                }
            return false;
            }

        public bool Contains(Guid identifier)
            {
            var style = Get(identifier);
            return style != null;
            }

        /// <summary>
        /// Creates a new style, or effectively edits an existing style.
        /// To create a style, supply an instance of the Style class that has been constructed.
        /// To edit an existing style, retrieve it from the database first, modify its fields, then supply that instance instead.
        /// </summary>
        /// <param name="style">The style to write to the database.</param>
        public void Write(Style style)
            {
            Database.Write(style);
            }

        public void Delete(Style style)
            {
            Database.Delete(style);
            }

        }

    }
